package com.reebake.ideal.security.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjUtil;
import com.reebake.ideal.permission.client.RoleClient;
import com.reebake.ideal.security.entity.UserDetailsEntity;
import com.reebake.ideal.security.entity.UserLoginLogEntity;
import com.reebake.ideal.security.entity.UserThirdPartyEntity;
import com.reebake.ideal.security.service.UserSecurityService;
import com.reebake.ideal.user.client.UserDetailsClient;
import com.reebake.ideal.user.client.UserLoginLogClient;
import com.reebake.ideal.user.client.UserThirdPartyClient;
import com.reebake.ideal.user.entity.UserLoginLogDTO;
import com.reebake.ideal.user.entity.UserThirdPartyDTO;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

public class UserSecurityServiceImpl implements UserSecurityService {
    @Autowired
    private UserDetailsClient userDetailsClient;
    @Autowired
    private UserThirdPartyClient userThirdPartyClient;
    @Autowired
    private UserLoginLogClient userLoginLogClient;
    @Autowired
    private RoleClient roleClient;

    private UserDetailsEntity fetchAuthInfo(UserDetailsEntity userDetails){
        if(ObjUtil.isNull(userDetails)){
            return userDetails;
        }
        List<String> roleCodes = roleClient.queryCodeByUserId(userDetails.getUserId());
        userDetails.setAuthorities(roleCodes);
        return userDetails;
    }

    @Override
    public UserDetailsEntity loadUserByUserId(String userId) {
        return fetchAuthInfo(BeanUtil.copyProperties(userDetailsClient.loadUserByUserId(userId), UserDetailsEntity.class));
    }

    @Override
    public UserDetailsEntity loadUserByPhoneNumber(String phoneNumber) {
        return fetchAuthInfo(BeanUtil.copyProperties(userDetailsClient.loadUserByPhoneNumber(phoneNumber), UserDetailsEntity.class));
    }

    @Override
    public UserDetailsEntity loadUserByEmail(String email) {
        return fetchAuthInfo(BeanUtil.copyProperties(userDetailsClient.loadUserByEmail(email), UserDetailsEntity.class));
    }

    @Override
    public UserDetailsEntity loadUserByUsername(String username) {
        return fetchAuthInfo(BeanUtil.copyProperties(userDetailsClient.loadUserByUsername(username), UserDetailsEntity.class));
    }

    @Override
    public UserDetailsEntity loadUserByThirdParty(String username, String registrationId) {
        return fetchAuthInfo(BeanUtil.copyProperties(userDetailsClient.loadUserByThirdParty(username, registrationId), UserDetailsEntity.class));
    }

    @Override
    public void bindThirdParty(UserThirdPartyEntity userThirdPartyEntity) {
        userThirdPartyClient.saveOrUpdate(BeanUtil.copyProperties(userThirdPartyEntity, UserThirdPartyDTO.class));
    }

    @Override
    public void log(UserLoginLogEntity userLoginLogEntity) {
        userLoginLogClient.log(BeanUtil.copyProperties(userLoginLogEntity, UserLoginLogDTO.class));
    }

    @Override
    public void lock(String username) {
        userDetailsClient.lock(username);
    }

    @Override
    public UserDetailsEntity register(String username) {
        String userId = userDetailsClient.simpleRegister(username);
        return this.loadUserByUserId(userId);
    }
}
